## Replication of Figure E.1 and Figure E.2 of Hollenbach and Silva (2018)
## Fiscal Capacity and Inequality: Evidence from Brazilian Municipalities
## Hollenbach, Florian and Thiago Silva
## Last update: April, 2018



library(readstata13)
library(ggplot2)  
library(ggthemes)

#set working directory, load data

subsample <- read.dta13("master.dta")

## Replication of Figure E.1
## Creating plot of density for municipalities age
pdf("FigureE1_MunicipalityAge.pdf")
ggplot(subsample, aes(x=municdiff2010)) + geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                           color="black", fill="lightgray") + theme_few()  +
  ylab("Density") + xlab("Municipality Age") + 
  geom_density(alpha=.2, fill="lightblue")
dev.off()

# Creating plots of density for inequality for different municipal years

# All Data
mall = ggplot(subsample, aes(x=gini)) + 
  geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                 color="black", fill="lightgray") + theme_few()  +
  ylab("Density") + xlab("Gini Coefficient") + 
  geom_density(alpha=.2, fill="lightblue")  +  # Overlay with transparent density plot
  ggtitle("All Data")

# Municipalities between 0 and 10 years old
munic010 <- subset(subsample, municdiff2010>0 & municdiff2010<11)
m010 = ggplot(munic010, aes(x=gini)) + 
  geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                 color="black", fill="lightgray") + theme_few()  +
  ylab("Density") + xlab("Gini Coefficient") + 
  geom_density(alpha=.2, fill="lightblue") +  # Overlay with transparent density plot
  ggtitle("0 to 10 Years Old")

# Municipalities between 11 and 20 years old
munic1120 <- subset(subsample, municdiff2010>10 & municdiff2010<21)
m1120 = ggplot(munic1120, aes(x=gini)) + 
  geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                 color="black", fill="lightgray") + theme_few()  +
  ylab("Density") + xlab("Gini Coefficient") + 
  geom_density(alpha=.2, fill="lightblue") +  # Overlay with transparent density plot
  ggtitle("11 to 20 Years Old")

# Municipalities between 21 and 30 years old
munic2130 <- subset(subsample, municdiff2010>20 & municdiff2010<31)
m2130 = ggplot(munic2130, aes(x=gini)) + 
  geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                 color="black", fill="lightgray") + theme_few()  +
  ylab("Density") + xlab("Gini Coefficient") + 
  geom_density(alpha=.2, fill="lightblue")  +  # Overlay with transparent density plot
  ggtitle("21 to 30 Years Old")

# Municipalities between 31 and 40 years old
munic3140 <- subset(subsample, municdiff2010>30 & municdiff2010<41)
m3140 = ggplot(munic3140, aes(x=gini)) + 
  geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                 color="black", fill="lightgray") + theme_few()  +
  ylab("Density") + xlab("Gini Coefficient") + 
  geom_density(alpha=.2, fill="lightblue")  +  # Overlay with transparent density plot
  ggtitle("31 to 40 Years Old")

# Municipalities between 41 and 50 years old
munic4150 <- subset(subsample, municdiff2010>40 & municdiff2010<51)
m4150 = ggplot(munic4150, aes(x=gini)) + 
  geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                 color="black", fill="lightgray") + theme_few()  +
  ylab("Density") + xlab("Gini Coefficient") + 
  geom_density(alpha=.2, fill="lightblue")  +  # Overlay with transparent density plot
  ggtitle("41 to 50 Years Old")

# Municipalities between 51 and 60 years old
munic5160 <- subset(subsample, municdiff2010>50 & municdiff2010<61)
m5160 = ggplot(munic5160, aes(x=gini)) + 
  geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                 color="black", fill="lightgray") + theme_few()  +
  ylab("Density") + xlab("Gini Coefficient") + 
  geom_density(alpha=.2, fill="lightblue")  +  # Overlay with transparent density plot
  ggtitle("51 to 60 Years Old")

# Municipalities between 61 and 70 years old
munic6170 <- subset(subsample, municdiff2010>60 & municdiff2010<71)
m6170 = ggplot(munic6170, aes(x=gini)) + 
  geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                 color="black", fill="lightgray") + theme_few()  +
  ylab("Density") + xlab("Gini Coefficient") + 
  geom_density(alpha=.2, fill="lightblue")  +  # Overlay with transparent density plot
  ggtitle("61 to 70 Years Old")

# Municipalities between 71 and 77 years old
munic7177 <- subset(subsample, municdiff2010>70 & municdiff2010<78)
m7177 = ggplot(munic7177, aes(x=gini)) + 
  geom_histogram(aes(y=..density..),      # Histogram with density instead of count on y-axis
                 color="black", fill="lightgray") + theme_few()  +
  ylab("Density") + xlab("Gini Coefficient") + 
  geom_density(alpha=.2, fill="lightblue")  +  # Overlay with transparent density plot
  ggtitle("71 to 77 Years Old")

library(gridExtra)   #package needed for the command grid.arrange

## Replication of Figure E.2
pdf("FigureE2_GiniByCityYear.pdf")

grid.arrange(mall, m010, m1120, m2130, m3140, m4150, m5160, m6170, m7177) #to divide by column (side-by-side) add ", ncol=2" within the parenthesis
dev.off()